//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_ALGORITHM_CASE_H_
#define _BUOLA_ALGORITHM_CASE_H_

#include <buola/iterator/range.h>
#include <buola/iterator/transform.h>
#include <buola/functors/transform/case.h>
#include <buola/utility/ulocale.h>

namespace buola {

///\addtogroup algorithm
///@{

///\name Case conversion
//@{

///converts all elements to lowercase
///
///\param pRange a range which will be transformed in place
///\param pLocale the locale used for the conversion. The current locale will be used by default.
///
///Note that, since this function operates on characters one at a time, it will not correctly transform non-ASCII
///characters in a UTF-8 encoded string. To perform such a conversion, use `wstring`
template<typename tRange>
inline void tolower(tRange &pRange,const ULocale &pLocale=ULocale())
{
    auto lRange=range(pRange);
    std::transform(range_begin(lRange),range_end(lRange),range_begin(lRange),fn::AToLower<ORangeValue<tRange>>(pLocale));
}

///converts all elements to uppercase
///
///\param pRange a range which will be transformed in place
///\param pLocale the locale used for the conversion. The current locale will be used by default.
///
///Note that, since this function operates on characters one at a time, it will not correctly transform non-ASCII
///characters in a UTF-8 encoded string. To perform such a conversion, use `wstring`
template<typename tRange>
inline void toupper(tRange &pRange,const ULocale &pLocale=ULocale())
{
    auto lRange=range(pRange);
    std::transform(range_begin(lRange),range_end(lRange),range_begin(lRange),fn::AToUpper<ORangeValue<tRange>>(pLocale));
}

///converts all elements to lowercase (cloning version)
///
///\param pRange a range which contains the characters to transform
///\param pLocale the locale used for the conversion. The current locale will be used by default.
///\return a new sequence with all the characters of \p pRange converted to lowercase
///
///Note that, since this function operates on characters one at a time, it will not correctly transform non-ASCII
///characters in a UTF-8 encoded string. To perform such a conversion, use `wstring`
///
///\sa range_clone, which is used to create the new sequence
template<typename tSrc>
inline ORangeCloneSequence<tSrc> tolower_clone(const tSrc &pSrc,const ULocale &pLocale=ULocale())
{
    return range_clone(transform_range(pSrc,fn::AToLower<ORangeValue<tSrc>>(pLocale)));
}

///converts all elements to uppercase (cloning version)
///
///\param pRange a range which contains the characters to transform
///\param pLocale the locale used for the conversion. The current locale will be used by default.
///\return a new sequence with all the characters of \p pRange converted to lowercase
///
///Note that, since this function operates on characters one at a time, it will not correctly transform non-ASCII
///characters in a UTF-8 encoded string. To perform such a conversion, use `wstring`
///
///\sa range_clone, which is used to create the new sequence
///
///Example usage:
///~~~~~~~~~~~~~~
///std::wstring lStr(L"naïve");
///msg_info() << toupper_clone(lStr) << "\\n"; //will print "NAÏVE"
///~~~~~~~~~~~~~~
template<typename tSrc>
inline ORangeCloneSequence<tSrc> toupper_clone(const tSrc &pSrc,const ULocale &pLocale=ULocale())
{
    return range_clone(transform_range(pSrc,fn::AToUpper<ORangeValue<tSrc>>(pLocale)));
}

//@}

///@}

/*namespace buola*/ }

#endif
